<template>
	<view class="index">
		<view class="form-wrapper">
			<view class="form-title">
				原管理员
			</view>
			<view class="form-item">
				<view class="label">
					姓名
				</view>
				<input type="text" disabled v-model="params.staffName" class="input" />
			</view>
			<view class="form-item">
				<view class="label">
					手机
				</view>
				<input type="text" disabled v-model="params.staffTel" class="input" />
			</view>
		</view>

		<view class="form-wrapper">
			<view class="form-title">
				更换为
			</view>
			<view class="staff-item" @click="selectIndex = index" :class="{'current':index === selectIndex}"
				v-for="(item,index) in list" :key="index">
				<uni-icons type="checkbox-filled" v-if="index === selectIndex" size="24" color="#F26022"></uni-icons>
				<uni-icons type="circle" v-else size="24" color="#ababab"></uni-icons>
				<view class="info">
					<view class="form-item">
						<view class="label">
							姓名
						</view>
						<input type="text" disabled v-model="item.staffName" class="input" />
					</view>
					<view class="form-item">
						<view class="label">
							手机
						</view>
						<input type="text" disabled v-model="item.staffTel" class="input" />
					</view>
				</view>
			</view>
		</view>

		<view class="tip">
			注：更换管理员以后，您将自动退出退出系统。同时失去所有功能访问权限，需由新管理员重新为您授权。
		</view>

		<view class="submit-btn" @click="submit">
			确定
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				params: {},
				list: [],
				selectIndex: null
			}
		},
		onLoad(option) {
			this.params = JSON.parse(option.params)
			console.log(this.params)
			this.getList()
		},
		methods: {
			async getList() {
				const data = await this.$api.post('/app/city-warehouse-staff/page', {
					pageSize: 50,
					pageNum: 1,
					orders: [{
						"asc": true,
						"column": "id"
					}]
				})
				if (data.code === 200) {
					this.list = data.data.records.filter(item => item.state === 0 && !item.isAdmin)
				}
			},
			submit() {
				if (this.selectIndex === null) {
					uni.showToast({
						title: '请选择一个员工进行更换',
						icon: 'none',
						duration: 2000
					})
					return
				}
				uni.showModal({
					title: '请确认',
					content: '更换管理员以后，您将自动退出退出系统。同时失去所有功能访问权限，需由新管理员重新为您授权。',
					success: async (e) => {
						if (e.confirm) {
							const obj = this.list[this.selectIndex];
							const data = await this.$api.post('/app/city-warehouse-staff/changeToAdmin', {
								id: obj.id
							})
							if (data.code === 200) {
								this.$store.commit('setToken', '')
								uni.reLaunch({
									url: '/pages/login/index'
								})
							}
						}
					}
				})
			}
		},
		components: {

		}
	}
</script>

<style lang="scss" scoped>
	.index {
		height: 100%;
		overflow: auto;
		padding: 0 24upx;
		padding-bottom: 150rpx;

		.form-wrapper {
			background-color: #ffffff;
			border-radius: 10upx;
			padding: 30upx;
			margin-top: 20upx;

			.form-title {
				font-size: 32upx;
				font-weight: bold;
				margin-bottom: 20upx;
			}

			.staff-item {
				padding: 20rpx 30rpx;
				border-bottom: 1rpx solid #efefef;
				background-color: #f5f5f5;
				margin-bottom: 20rpx;
				border-radius: 10rpx;
				display: flex;
				align-items: center;

				&.current {
					box-shadow: 0 0 10rpx #F26022ce;
					background-color: #F2602210;
				}

				&:last-child {
					border: 0;
				}

				.info {
					margin-left: 30rpx;
				}

				.form-item {
					border: 0;
					height: 60rpx;
				}
			}

			.form-item {
				display: flex;
				align-items: center;
				height: 90upx;
				border-bottom: 1upx solid #efefef;

				.label {
					font-size: 32upx;
					width: 140upx;
				}

				.input {
					flex: 1;
				}

				.btn {
					padding: 8upx 20upx;
					border: 1upx solid #bebebe;
					border-radius: 10upx;
				}
			}
		}

		.tip {
			font-size: 28rpx;
			color: #666666;
			margin-top: 20rpx;
		}

		.submit-btn {
			position: fixed;
			height: 90upx;
			width: 100%;
			bottom: 0;
			left: 0;
			background-color: #F26022;
			color: #ffffff;
			display: flex;
			align-items: center;
			justify-content: center;
			z-index: 1;
		}
	}
</style>